<?php

/**
 * Mapping table [cdb_forums]
 *
 * @category   app_M
 * @package    app_M
 * @author     
 * @copyright  Copyright (c)
 * @license
 */
class Forums extends Zend_Db_Table
{
    /**
     * The table name.
     *
     * @var string
     */
	protected function _setup()
	{
		$config = Zend_Registry::get('config');
		$this->_name = $config->db->config->prcode.'forums';
		$this->_request = new Zend_Controller_Request_Http();
		parent::_setup();
	}
	/**
      *论坛由board_name找fid
      */
	public function category_boardnametofid($board_name = 0){
		 $where = $this->getAdapter()->quoteInto('board_name = ?', $board_name);
		 $order = 'displayorder';
         $rowset =  $this->fetchRow($where, $order);
		 return $rowset->toArray();
		
	}
	/**
      *从cdb_forumfields取出版块的详细信息
      */
	public function category_getforumfields($fid = 0){
		 $where = $this->getAdapter()->quoteInto('fid = ?', $fid);
		 $rowset =  $this->fetchRow($where);
		 return $rowset->toArray();
	}
	/**
      *论坛分类处理
      */
	public function category_tree($cat_id = 0)
	{
		if ($cat_id > 0)
        {
		    $where = $this->getAdapter()->quoteInto('fid = ?',$cat_id);
		    $rows =$this->fetchRow($where);
		    $fup = $rows->fup;
		}else {
		    $fup = 0;
		}
		/*
		判断当前分类中全是是否是底级分类，
		如果是取出底级分类上级分类，
		如果不是取当前分类及其下的子分类
		*/
		$where = $this->getAdapter()->quoteInto('fup = ?',$fup);
		$result =$this->fetchAll($where)->toArray();
		if ($result==null) {
				/* 获取当前分类及其父分类 */
			$sql = 'SELECT a.fid, a.name, b.fid AS child_id, b.name AS child_name, b.displayorder ' .
			'FROM ' .$this->_name. ' AS a ' .
			'LEFT JOIN ' .$this->_name. ' AS b ON b.fup = a.fid ' .
			" WHERE b.fup = '$fup' b.ORDER BY b.displayorder ASC";
			
		}else {
			/* 获取当前分类及其子分类 */
			$sql = 'SELECT a.fid,a.name,a.displayorder AS parent_order,' .
			'b.fid AS child_id, b.name AS child_name,b.board_name AS child_board_name, b.displayorder AS child_order ' .
			'FROM '  .$this->_name.  ' AS a ' .
			'LEFT JOIN ' .$this->_name. ' AS b ON b.fup = a.fid ' .
			"WHERE a.fup = '$fup' ORDER BY parent_order ASC, a.fid ASC, child_order ASC";
		}
		$res = $this->_db->fetchAll($sql);
		$cat_arr = array();
		foreach ($res AS $row)
		{
			$cat_arr[$row['fid']]['fid']   = $row['fid'];
			$cat_arr[$row['fid']]['name'] = $row['name'];
			$cat_arr[$row['fid']]['url'] = 'Category/'.$row['fid'];
			if ($row['child_id'] != NULL)
			{
				 $cat_arr[$row['fid']]['children'][$row['child_id']]['fid']   = $row['child_id'];
                 $cat_arr[$row['fid']]['children'][$row['child_id']]['name'] = $row['child_name'];
                 $cat_arr[$row['fid']]['children'][$row['child_id']]['url'] = 'Category/'.$row['child_board_name'];
			
			}
		}
		return $cat_arr;
	}
	
}